﻿#include "SkillSomeSummon.h"
#include "Character/SkillPart.h"
#include "base/core/log.h"
#include "base/core/com_define.h"
#include "base/core/utility.h"
#include "Scene/MapMgr.h"
#include "Character/CreatureMgr.h"
#include "Character/monster/Monster.h"
#include "Character/npc/Npc.h"
SkillSomeSummon::SkillSomeSummon()
{

}
SkillSomeSummon::~SkillSomeSummon()
{

}


void SkillSomeSummon::OnDamageTimedo(uint32_t skillId)
{

	std::vector<string> vStr;
	cList  targetCidList;
	stSkillInfo* pSkill = m_pPart->GetSkillInfo(skillId);
	MMOLOG_PROCESS_ERROR(pSkill != nullptr);
	m_pPart->KillTimedo(m_nGlobelID);
	//根据技能参数，创建怪物
	MMOLOG_ASSERT_EXIT(pSkill->_pSkillmeta->summonType == SkillSummonType_SkillSomeSummon);
	
	if (m_curDamageCount == 0)
	{
		//解析怪物位置
		CommonApi::SplitStr(pSkill->_pSkillmeta->summonValue, ";", &vStr);

		for (int i = 0; i < (int)vStr.size(); ++i)
		{
			std::vector<int32_t> vID;
			CommonApi::SplitStrToVecInt(vStr[i], ",", &vID);
			if ((int)vID.size() != 3)
			{
				LogDebugFmtPrint("skllid=%d  summon parm is not mod 3==0", skillId);
				continue;
			}
			//创建怪物
			uint32_t monsterid = vID[2];
			float rangle = vID[0] / 1000.0f;    //弧度
			float radius = vID[1] / 1000.0f;  //米

			//计算坐标
			Point3<float> targetPos;// m_pPart->GetMasterPos() + m_pPart->GetMasterDir()*result[0] / 1000.0f;
			Point3<float> srcPos = m_pPart->GetMasterPos();
			targetPos = srcPos;
			Map* pMap = g_GetMapMgr()->GetMap(m_pPart->GetMaster()->GetMapId());
			bool ret = false;
			Point3<float> tmpdir;
			if (pMap)
			{
				// x1 = x0 * cosB + y0 * sinB
				// y1 = -x0 * sinB + y0 * cosB

				tmpdir.x = m_pPart->GetMasterDir().x*cos(AngleToRadian(rangle)) + m_pPart->GetMasterDir().z*sin(AngleToRadian(rangle));
				tmpdir.z = -1 * m_pPart->GetMasterDir().x * sin(AngleToRadian(rangle)) + m_pPart->GetMasterDir().z*cos(AngleToRadian(rangle));
				tmpdir.y = 0;
				for (float d = radius; d >= 0.1f; d -= 0.1f)
				{
					Point3<float> tmpPos = srcPos + tmpdir * d;
					targetPos = srcPos;
					ret = pMap->FindNearestPos(tmpPos.x, tmpPos.z, tmpPos.y, &targetPos.x, &targetPos.z, &targetPos.y, nullptr);
					if (ret)
					{
						break;
					}
				}
			}
			MonsCreateParam createParam;
			createParam.createType = EMonsCreateType_SummonSkill;
			createParam.createTypeVal = m_pPart->GetMasterCid();
			Monster* pMonster = g_GetCreatureMgr()->CreateMonster(monsterid, m_pPart->GetMaster()->GetSceneId(), targetPos, createParam);
			if (!pMonster)
			{
				LogDebugFmtPrint("skill=%d summon Monster error", skillId);
				goto Exit0;
			}
			pMonster->SetMonsterMasterCID(m_pPart->GetMasterCid());
			pMonster->SetMonsterMasterKind(m_pPart->GetMaster()->CreatureKind());
			if (m_pPart->GetMaster()->CreatureKind() == CREATURE_PLAYER)
			{
				pMonster->SetMonsterCampID(m_pPart->GetMaster()->GetAttr(C_CAMP_ID));
			}
			else if (m_pPart->GetMaster()->CreatureKind() == CREATURE_MONSTER)
			{
				Monster* pOwner = dynamic_cast<Monster*>(m_pPart->GetMaster());
				if(pOwner)
				{
					pMonster->SetMonsterCampID(pOwner->GetMonsterCampID());
				}
			}
			else if (m_pPart->GetMaster()->CreatureKind() == CREATURE_NPC)
			{
				Npc* pNpc = dynamic_cast<Npc*>(m_pPart->GetMaster());
				if (pNpc)
				{
					pMonster->SetMonsterCampID(pNpc->GetNpcInfo()->npcCamp);
				}
			}

			m_pPart->GetMaster()->AddSummon(pMonster->GetCid());
		}
	}

	m_pPart->FindCreatures(pSkill->_pSkillmeta, m_pPart->GetMasterPos(), m_skillDir, 0, m_pPart->GetMasterPos(), m_curDamageCount, m_focoTime, targetCidList);

	//计算技能结果
	m_pPart->SkillResult(skillId, pSkill->_pSkillmeta, m_curDamageCount, m_focoTime, targetCidList);

	if ((uint32_t)pSkill->_pSkillmeta->damageTime.size() > m_curDamageCount + 1)
	{
		m_curDamageCount++;
		m_pPart->SetTimedo(m_nGlobelID, pSkill->_pSkillmeta->damageTime[m_curDamageCount], 1);
	}
	else
	{

		//技能一次释放完成，需要删通知删除
		m_pPart->RemoveSkillFightInfo(m_nGlobelID);
	}

	//m_pPart->RemoveSkillFightInfo(m_nGlobelID);
	return;
Exit0:
	//m_pPart->KillTimedo(m_nGlobelID);
	m_pPart->RemoveSkillFightInfo(m_nGlobelID);
	return;
}
